<?php

namespace addons\shopro\job;

use addons\shopro\service\user\User as UserService;
use addons\shopro\service\Wallet as WalletService;
use app\admin\model\Children;
use app\common\model\shopro\UserNormalPrizeLogModel;
use app\common\model\shopro\UserTeamDayWalletLogModel;
use app\common\model\Userchildren;
use think\queue\Job;
use think\Db;
use think\exception\HttpResponseException;
/**
 * 砸金蛋后动作
 */
class PrizePaid extends BaseJob
{

    /**
     * 砸金蛋后
     */
    public function paid(Job $job, $data)
    {
        try {
            Db::transaction(function () use ($data) {
                $prize_money      = $data['prize_money'];
                $user             = $data['user'];
                $prizeAppointInfo = $data['prizeAppointInfo'];
                $userModel        = new \app\common\model\User();
                $userModel->where(['id' => $user['id']])->setInc('user_water_log', $prize_money);
                if ($user['parent_user_id'] > 0) {
                    //给团队增加总业绩
                    $this->team_yeji($user, $prize_money, $userModel);
                    if ($user['level'] == 0) {
                        $this->user_normal($user, $prize_money, $prizeAppointInfo, $userModel);
                    }
                    jicha($user, $prize_money);
                }
            });
            //升级
            $job->delete();
        } catch (HttpResponseException $e) {
            $data    = $e->getResponse()->getData();
            $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
            format_log_error($e, 'PrizePaid.paid.HttpResponseException', $message);
        } catch (\Exception $e) {
            // 队列执行失败
            format_log_error($e, 'PrizePaid.paid');
        }
    }

    /**
     * @menu:普通会员奖励
     * @Author:男人的坏
     * @Date:2024-07-20 11:49
     */

    public function user_normal($user, $prize_money, $prizeAppointInfo, $userModel)
    {
//        $time             = date('Ymd');
//        $user_create_time = date('Ymd', $user['createtime']);
//        if($time != $user_create_time){
            $user_ids_arr = Userchildren::where(['children_id' => $user['id']])->column('member_id');
//            $userData     = $userModel->whereIn('id', $user_ids_arr)->where('is_qrcode', '=',
//                1)->where('is_ptfc','=',1)->field('id,total_consume,level')->order('id desc')->select();
        $userData     = $userModel->whereIn('id', $user_ids_arr)->where('is_qrcode', '=',
            1)->field('id,total_consume,level')->order('id desc')->select();
            if ($userData) {
                foreach ($userData as $k => $v) {
                    $count = $userModel->where(['parent_user_id' => $v['id'], 'is_qrcode' => 1])->count();
                    if ($count >= 3) {
                        $money = bcmul($prize_money, config('site.game_dividend') / 100);
                        if ($money) {
                            WalletService::change($v['id'], 'score', $money, 'game_dividend', [],
                                '团队游戏流水返利');
                        }
                        if ($prizeAppointInfo['type'] == 2) {
                            $inser['user_id']      = $v['id'];
                            $inser['draw_user_id'] = $user['id'];
                            $inser['prize_id']     = $prizeAppointInfo['id'];
                            UserNormalPrizeLogModel::create($inser);
                        }
                        if($v['level'] > 0){
                            break;
                        }
                    }
                }
            }
//        }
    }

    /**
     * @menu:记录团队业绩
     * @Author:男人的坏
     * @Date:2024-07-20 09:46
     */
    public function team_yeji($user, $prize_money, $userModel)
    {
        $user_ids_arr = Userchildren::where(['children_id' => $user['id']])->column('member_id');
        $userData     = $userModel->whereIn('id', $user_ids_arr)->where('is_qrcode', '=',
            1)->field('id,total_consume')->select();
        if ($userData) {
            $dayLogModel = new UserTeamDayWalletLogModel();
            foreach ($userData as $k => $v) {
                $dayLogInfo = $dayLogModel->whereTime('createtime', 'd')->where(['user_id' => $v['id']])->find();
                if ($dayLogInfo) {
                    $dayLogInfo->amount += $prize_money;
                    $dayLogInfo->save();
                } else {
                    $inser['user_id'] = $v['id'];
                    $inser['endtime'] = strtotime(date('Y-m-d 00:00:01', strtotime("+1 day")));
                    $inser['amount']  = $prize_money;
                    $dayLogModel->create($inser);
                }
                $v->setInc('total_consume', $prize_money);
            }
            $this->Upgrades($userData, $userModel);
        }
    }

    /**
     * @menu 升级
     * @Author:男人的坏
     * @Date:2024-07-20 09:22
     */
    public function Upgrades($userData, $userModel)
    {
        foreach ($userData as $key => $user) {
            $parent_user_data = $userModel->where([
                'parent_user_id' => $user['id'],
                'is_qrcode'      => 1
            ])->field('id,total_consume + user_water_log as total')->order('total desc')->select();
            if (count($parent_user_data) >= 3) {
                $total_consume = 0;
                foreach ($parent_user_data as $k => $v) {
                    if ($k > 0) {
                        $total_consume += $v['total'];
                    }
                }
                if ($total_consume) {
                    $parent_user = $userModel->where(['id' => $user['id']])->find();
                    UserService::consume($parent_user, $total_consume);
                }
            }
        }

    }

}